package com.mooc.ppjoke.utils;

import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mooc.ppjoke.model.BottomBar;
import com.mooc.ppjoke.model.Destination;
import com.wfc.libcommon.AppGlobals;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

public class AppConfig {

    private static HashMap<String, Destination>  sDestConfig;
    private static BottomBar sBottomBar;

    public static HashMap<String,Destination> getDestConfig(){
        if(sDestConfig == null){
            String content=parseFile("destination.json");
            sDestConfig= JSON.parseObject(content,new TypeReference<HashMap<String, Destination>>() {});
        }
        return sDestConfig;

    }

    public static BottomBar getBottomBar(){
        if(sBottomBar==null){
            String content=parseFile("main_tabs_config.json");
            sBottomBar= JSON.parseObject(content,BottomBar.class);


        }
        return sBottomBar;
    }




    private static String parseFile(String fileName) {
        AssetManager assets = AppGlobals.getApplication().getAssets();
        InputStream is = null;
        BufferedReader br = null;
        StringBuilder builder = new StringBuilder();
        try {
            is = assets.open(fileName);
            br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (Exception e) {
                   e.printStackTrace();
            }
        }

        return builder.toString();
     }


}
